package chapter4.No4_1_使用reentrantLock类.demo2;

import java.util.concurrent.locks.ReentrantLock;

/**
 * !
 *
 * @author liujian
 * @version 2016/11/21 0021
 */
public class ServiceClass {
    ReentrantLock lock = new ReentrantLock();

    public void methodA() {
        lock.lock();
        try {
            System.out.println("线程" + Thread.currentThread().getName() + "开始执行");
            Thread.sleep(2000);
            System.out.println("线程" + Thread.currentThread().getName() + "结束执行");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        lock.unlock();
    }

    public void methodB() {
        lock.lock();
        try {
            System.out.println("线程" + Thread.currentThread().getName() + "开始执行");
            Thread.sleep(1000);
            System.out.println("线程" + Thread.currentThread().getName() + "结束执行");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        lock.unlock();
    }
}
